<?php
/**
 * Created by PhpStorm.
 * User: zhendanxu
 * Date: 2019/4/24
 * Time: 11:37 AM
 */

namespace AppBundle\Controller;

use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;

class LuckyController extends BaseController
{

    /**
     *
     */
    public function numberAction($count)
    {
        $numbers = array();
        for ($i = 0; $i < $count; $i++) {
            $numbers[] = rand(0, 100);
        }
        $numbersList = implode(', ', $numbers);

//        $html = $this->container->get('templating')->render(
//            'lucky/number.html.twig',
//            array('luckyNumberList' => $numbersList)
//        );
//
//        return new Response($html);

        //快捷方法
        return $this->render(
            'lucky/number.html.twig',
            array('luckyNumberList' => $numbersList)
        );
    }

    public function apiNumberAction()
    {
        $data = array(
            'lucky_number' => rand(0, 100)
        );

//        return new Response(
//            json_encode($data),
//            200,
//            array('Content-type' => 'appliction/json')
//        );
        //自动调用json_encode并设置Content-Type头
        return new JsonResponse($data);
    }

}